﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class UnitDAO : MarshalByRefObject, IUnit
    {
        protected DbHelper dbHelper = new DbHelper();

        //根据编码查询包装单位信息
        public MisModel.Unit GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from UNIT_TAB where UNIT='{0}'", id);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.Unit entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.Unit();
                entity.Code = dt.Rows[0]["UNIT"].ToString();
                entity.Name = dt.Rows[0]["UNITBEI"].ToString();
            }
            return entity;
        }

        //查询包装单位列表
        public DataTable GetAll()
        {
            string sql = "select UNIT,UNITBEI,(UNIT + '.' + UNITBEI) as ItemShow  from UNIT_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加包装单位信息
        public int Add(MisModel.Unit obj)
        {
            string sql = string.Format("insert into UNIT_TAB(UNIT,UNITBEI) values('{0}','{1}')", obj.Code, obj.Name);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改包装单位信息
        public int Update(MisModel.Unit obj)
        {
            string sql = string.Format("update UNIT_TAB set UNITBEI='{0}' where UNIT='{1}'", obj.Name, obj.Code);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除包装单位信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from UNIT_TAB where UNIT='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
